package com.android.fastride.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.fastride.Constant;
import com.android.fastride.R;
import com.android.fastride.activity.base.BaseActivity;
import com.android.fastride.manager.UserManager;
import com.android.fastride.model.db.entity.HomeIndicator;
import com.android.fastride.model.http.entity.LoginUserInfoEntity;
import com.android.fastride.view.fragment.DispatchCountFragment;
import com.android.fastride.view.fragment.MesssageCenterListFragment;
import com.android.fastride.view.fragment.OrderListFragment;
import com.android.fastride.view.fragment.RatingListFragment;
import com.android.fastride.view.fragment.SetFragment;
import com.kyleduo.switchbutton.SwitchButton;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MainActivity extends BaseActivity {

    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.view_pager)
    ViewPager view_pager;
    @BindView(R.id.ll_left_menus)
    LinearLayout llLeftMenus;
    @BindView(R.id.tb_main)
    Toolbar tbMain;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @BindView(R.id.bottom_navigation_bar)
    MagicIndicator bottomNavigationBar;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.sb_work_switch)
    SwitchButton sbWorkSwitch;
    @BindView(R.id.iv_to_be_completed)
    ImageView ivToBeCompleted;
    @BindView(R.id.tv_to_be_completed)
    TextView tvToBeCompleted;
    @BindView(R.id.iv_completed)
    ImageView ivCompleted;
    @BindView(R.id.tv_completed)
    TextView tvCompleted;
    @BindView(R.id.iv_dispatch_count)
    ImageView ivDispatchCount;
    @BindView(R.id.tv_dispatch_count)
    TextView tvDispatchCount;
    @BindView(R.id.iv_my_rate)
    ImageView ivMyRate;
    @BindView(R.id.tv_my_rate)
    TextView tvMyRate;
    @BindView(R.id.iv_set)
    ImageView ivSet;
    @BindView(R.id.tv_set)
    TextView tvSet;
    @BindView(R.id.iv_my_message)
    ImageView ivMyMessage;
    @BindView(R.id.tv_my_message)
    TextView tvMyMessage;
    @BindView(R.id.tv_help)
    TextView tvHelp;
    @BindView(R.id.iv_help)
    ImageView ivHelp;
    @BindView(R.id.tv_commission)
    TextView tvCommission;
    @BindView(R.id.tv_order_amount)
    TextView tvOrderAmount;
    private Unbinder unbinder = null;
    private List<HomeIndicator> mHomeIndicatorList;

    public static void start(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        unbinder = ButterKnife.bind(this);
        init();
    }

    /**
     * 初始化页面
     */
    private void init() {
//        initBottomNavigationBar();
        initMagicIndicator();
        initViewPager();
        initDrawerLayout();
//        initDate();
    }

    /**
     * 初始化数据
     */
    private void initDate() {
        LoginUserInfoEntity userInfoEntity = UserManager.getInstance().getUserInfo();
        tvUserName.setText(userInfoEntity.getName());
        tvCommission.setText(userInfoEntity.getCommission() + "");
        tvOrderAmount.setText(userInfoEntity.getOrderAmount() + "");
        sbWorkSwitch.setChecked(userInfoEntity.getStartState() == 1);

    }

    /**
     * 初始化列别页
     */
    private void initViewPager() {

        ArrayList<Fragment> fragmentArrayList = new ArrayList<>();
        fragmentArrayList.add(OrderListFragment.newInstance(Constant.ORDER_PENDING));
        fragmentArrayList.add(OrderListFragment.newInstance(Constant.ORDER_PICK_UP));
        fragmentArrayList.add(OrderListFragment.newInstance(Constant.ORDER_DISPATCH));

        view_pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentArrayList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentArrayList.size();
            }
        });
    }

    /**
     * 侧滑菜单栏
     */
    private void initDrawerLayout() {
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                //滑动过程中不断回调 slideOffset:0~1
                View content = drawerLayout.getChildAt(0);
                View menu = drawerView;

                float scale = 1 - slideOffset;//1~0
                content.setTranslationX(menu.getMeasuredWidth() * (1 - scale));//0~width
            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }

    private void initMagicIndicator() {
        mHomeIndicatorList = new ArrayList<HomeIndicator>();
        mHomeIndicatorList.add(new HomeIndicator("新任务", R.drawable.selector_icon_new_order, R
                .drawable
                .icon_new_order_off));
        mHomeIndicatorList.add(new HomeIndicator("待取货", R.drawable.selector_icon_pickup, R.drawable
                .icon_pickup_goods_off));
        mHomeIndicatorList.add(new HomeIndicator("配送中", R.drawable.selector_icon_deliver, R.drawable
                .icon_delivering_off));
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mHomeIndicatorList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, int i) {

                CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(context);

                // load custom layout
                View customLayout = LayoutInflater.from(context).inflate(R.layout
                        .item_home_navigation_layout, null);
                final ImageView titleImg = (ImageView) customLayout.findViewById(R.id.title_img);
                final TextView titleText = (TextView) customLayout.findViewById(R.id.title_text);
                titleText.setText(mHomeIndicatorList.get(i).getTitle());
                titleImg.setImageResource(mHomeIndicatorList.get(i).getImgSelectedSrc());
                commonPagerTitleView.setContentView(customLayout);
                commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView
                        .OnPagerTitleChangeListener() {
                    @Override
                    public void onSelected(int i, int i1) {
                        customLayout.setSelected(true);
                    }

                    @Override
                    public void onDeselected(int i, int i1) {
                        customLayout.setSelected(false);
                    }

                    @Override
                    public void onLeave(int i, int i1, float v, boolean b) {

                    }

                    @Override
                    public void onEnter(int i, int i1, float v, boolean b) {

                    }
                });

                commonPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        view_pager.setCurrentItem(i);
                    }
                });

                return commonPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        });

        bottomNavigationBar.setNavigator(commonNavigator);
        ViewPagerHelper.bind(bottomNavigationBar, view_pager);

        /*new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        commonNavigator.getAdapter().notifyDataSetChanged();
                    }
                });
            }
        }).start();*/

        commonNavigator.getAdapter().notifyDataSetChanged();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    @OnClick(R.id.iv_head)
    public void onViewClice(View view) {
        switch (view.getId()) {
            case R.id.iv_head:
                drawerLayout.openDrawer(llLeftMenus);
                break;
        }
    }

    @OnClick({R.id.iv_head, R.id.sb_work_switch, R.id.iv_to_be_completed, R.id
            .tv_to_be_completed, R.id.iv_completed, R.id.tv_completed, R.id.iv_dispatch_count, R
            .id.tv_dispatch_count, R.id.iv_my_rate, R.id.tv_my_rate, R.id.iv_set, R.id.tv_set, R
            .id.iv_my_message, R.id.tv_my_message, R.id.tv_help, R.id.iv_help})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_head:
                break;
            case R.id.sb_work_switch:
                break;
            case R.id.iv_to_be_completed:
            case R.id.tv_to_be_completed:
                // 待完成
                break;
            case R.id.iv_completed:
            case R.id.tv_completed:
                // 已完成
                CompletedOrderListActivity.start(this);
                break;
            case R.id.iv_dispatch_count:
            case R.id.tv_dispatch_count:
                // 配送统计
                ProxyFragmentActivity.start(this, DispatchCountFragment.class);
                break;
            case R.id.iv_my_rate:
            case R.id.tv_my_rate:
                // 我的评价
                ProxyFragmentActivity.start(this, RatingListFragment.class);
                break;
            case R.id.iv_set:
            case R.id.tv_set:
                // 设置
                ProxyFragmentActivity.start(this, SetFragment.class);
                break;
            case R.id.iv_my_message:
            case R.id.tv_my_message:
                // 我的消息
                ProxyFragmentActivity.start(this, MesssageCenterListFragment.class);
//                ProxyFragmentActivity.start(this, TestListFragment.class);
                break;
            case R.id.tv_help:
            case R.id.iv_help:
                // 帮助
                HelpActivity.start(this);
                break;
        }
    }
}
